package com.book.iphone;

import com.book.book.domain.ApplyDO;
import com.book.book.domain.BooksDO;
import com.book.book.domain.CommunicateDO;
import com.book.book.service.ApplyService;
import com.book.book.service.BooksService;
import com.book.book.service.CommunicateService;
import com.book.common.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：青松
 * 日期： 2018/1/30
 */
//讨论区
@SuppressWarnings("Duplicates")
@RequestMapping("/iphone")
@Controller
public class ExchangeController {
    @Autowired
    CommunicateService communicateService;
    @Autowired
    BooksService booksService;
    @Autowired
    ApplyService applyService;

     /**
     * 取出数据库中的书
     * 不进行分组
     * 显示漂流用户以及推荐语
     * @param model
     * @return
     */
    @GetMapping("/exchange")
    String getexchange(Model model){
        Map map = new HashMap();
        //取出的条数
        map.put("limit",10);
        //取出的页数
        map.put("offset",0);
        //根据书名进行分组
        map.put("sortOrder","bookId");
        List<CommunicateDO> communicateDOList= communicateService.list(map);
        model.addAttribute("communicateDOList",communicateDOList);
        return "book/phone/html/exchange";
    }
    /**
     * 图书详情页
     *根据图书id 查询这本书的所有评论
     * @param model
     * @param bookid
     * @return
     */

    @RequestMapping("/exchange/{bookid}") //根据书名拿到所有的评论
    String  getBookCommunicate (Model model, @PathVariable("bookid") int bookid) {
        //   根据图书id 查询这本书的所有评论
        Map<String, Object> map = new HashMap();
        map.put("limit", 10);
        map.put("offset", 0);
        map.put("bookId", bookid);
         //Query query = new Query(map);
        ApplyDO applyDO= applyService.get(bookid);

        if( applyDO.getBookImg().equals("")){
            applyDO.setBookImg("");
         }
        applyDO.setBookImg(applyDO.getBookImg().split(",")[0]);
        List<CommunicateDO> bookCommunicate = communicateService.listWithName(map);
        model.addAttribute("bookCommunicate", bookCommunicate);
        model.addAttribute("books",applyDO);
        return "book/phone/html/exchange";
    }
}
